package com.tsinghuabigdata.edu.ddmath.module.neteaseim.education.helper;

import android.content.Context;
import android.graphics.Bitmap;

import com.squareup.picasso.Callback;
import com.tsinghuabigdata.edu.ddmath.util.AppLog;
import com.tsinghuabigdata.edu.ddmath.util.PicassoUtil;

import java.util.ArrayList;

/**
 * 图像下载管理类
 */
@Deprecated
public class ImageManagerHelper {

    private Context mContext;
    private ArrayList<DownloadListener> listeners;

    private static ImageManagerHelper instance;
    public static final ImageManagerHelper getInstance(Context context/*, DownloadListener listener*/){
        if( instance == null ){
            instance = new ImageManagerHelper( context );
        }
//        instance.registerDownloadListener( listener );
        return instance;
    }


     /**
     * 预下载图片
     * @param url 地址
     */
     public void preDownloadImage( String url ){
        (new PreDownImage( url )).dowmload();
    }

    public void registerDownloadListener( DownloadListener listener ){
        if( !listeners.contains( listener) )
            listeners.add( listener );
    }
    public void unregisterDownloadListener( DownloadListener listener ){
        listeners.remove( listener );
    }

    public Bitmap getBitmap( String url ){
        try {
            return PicassoUtil.getPicasso(mContext).load( url ).get();
        }catch (Exception e){
            AppLog.i( e.toString() );
        }
        return null;
    }

    //--------------------------------------------------------------------------
    //隐藏构造函数
    private ImageManagerHelper( Context context ){
        mContext = context;
        listeners = new ArrayList<>();
    }

    private void callbackListener( boolean success, String url, Bitmap bitmap ){
        for( DownloadListener listener : listeners ){
            if( success ){
                listener.downloadSuccess( url, bitmap );
            }else{
                listener.downloadFailure( url );
            }
        }
    }

    //--------------------------------------------------------------------------
    public interface DownloadListener{
        void downloadSuccess( String url, Bitmap bitmap );
        void downloadFailure( String url );
    }
//    //管理图片下载状态
//    class CountManager{
//
//        private Object lock;
//        private int conut;
//
//        public CountManager( Object obj ){
//            lock = obj;
//            conut = 0;
//        }
//
//        public void addCount(){
//            synchronized (lock){
//                conut++;
//            }
//        }
//        public void downCount(){
//            synchronized (lock){
//                conut--;
//            }
//        }
//
//        public boolean finish(){
//            return conut<=0;
//        }
//    }

    /**
     * 预下载图片
     */
    class PreDownImage{

        private int try_count = 0;
        private String url = "";

        public PreDownImage( String url ){
            this.url = url;

            //countManager.addCount();
        }

        public void dowmload(){
            PicassoUtil.getPicasso(mContext).load( url ).fetch(new Callback() {
                @Override
                public void onSuccess() {
                    try {
                        Bitmap bitmap = PicassoUtil.getPicasso(mContext).load( url).get();
                        callbackListener( true, url, bitmap );
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    callbackListener( false, url, null );
                }

                @Override
                public void onError() {
                    try_count++;
                    if (try_count < 3) {      //1分钟内重试12次
                        //AppLog.d("---fskdjfkdsjfkdsjkf try_count = " + try_count  );
                        new android.os.Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                dowmload();
                            }
                        }, 10 );
                    }else{
                        callbackListener( false, url, null );
                    }
                }
            });
        }
    }

}
